package com.wangijun.yuan.core.web;


import cn.hutool.core.lang.Dict;
import cn.hutool.json.JSONUtil;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;

import java.io.IOException;

/**
 * @author mldong
 * @date 2023/9/20
 */
public class YuanFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        YuanRequestWrapper yuanRequestWrapper = new YuanRequestWrapper((HttpServletRequest) servletRequest);
        String body = yuanRequestWrapper.getBody();
        if (JSONUtil.isTypeJSONObject(body)) {
            QueryParamHolder.set(JSONUtil.toBean(body, Dict.class));
        } else {
            QueryParamHolder.set(Dict.create());
        }
        filterChain.doFilter(yuanRequestWrapper, servletResponse);
    }
}
